var util = require('../../utils/util.js');
var api = require('../../config/api.js');

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    list: [],
    backUrl: '',
    height: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (options.id) {
      that.setData({
        id: parseInt(options.id)
      });
      //去后端查询本广告对应的商品列表
      util.request(api.goodsByAd, {
          id: this.data.id
        })
        .then(function(res) {
          if (res.errno == 0) {
            that.setData({
              list: res.data.goods,
              backUrl: res.data.backUrl
            });

            if (that.data.list.length == 1) {
              that.setData({
                height: '210px'
              })
            } else if (that.data.list.length == 2) {
              that.setData({
                height: '430px'
              })
            } else if (that.data.list.length == 3) {
              that.setData({
                height: '680px'
              })
            } else if (that.data.list.length == 4) {
              that.setData({
                height: '900px'
              })
            } else if (that.data.list.length == 5) {
              that.setData({
                height: '1130px'
              })
            } else if (that.data.list.length == 6) {
              that.setData({
                height: '1330px'
              })
            } else if (that.data.list.length == 7) {
              that.setData({
                height: '1580px'
              })
            } else if (that.data.list.length == 8) {
              that.setData({
                height: '1800px'
              })
            }
          }
        })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})